package com.xt.baselib;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;

import com.xt.baselib.listener.OnMediaListener;
import com.xt.baselib.listener.OnMediaPlayer;

import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
import java.util.Locale;

import tv.danmaku.ijk.media.example.application.Settings;
import tv.danmaku.ijk.media.example.widget.media.IjkVideoView;
import tv.danmaku.ijk.media.exo.IjkExoMediaPlayer;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import tv.danmaku.ijk.media.player.MediaPlayerProxy;

/**
 * Created by xiaolu on 2022/8/16
 * Email: xiaolu_008@126.com
 * <p>
 * ijkPlayer的封装
 */
public class IVideoPlayer extends LinearLayout implements View.OnClickListener {
    private final String TAG = "NurVideoPlayer";
    private IMediaPlayer iMediaPlayer = null;
    ;
    private final float MAX_LIANG_DU = 255f;
    private final AudioManager am;
    private final float maxVolume;
    private IjkVideoView mVideoView;
    private OnMediaPlayer mOnMediaPlayer;
    private View mTitleControl, mBottomControl, mVolumeControl, mCenterSBLayout, mLayoutBox, mProgressBar, mCenterPlayBtn;
    private ImageView mBgImage, mBackIv, mVolumeIV, mScreenView, mLockImage, mRCImage, volumeIcon, mVideoSeekBarImage;

    private TextView mVideoMaxLenTv, mTitleView, mVideoDurationTv, mVideoSeekBarTimeTv, mVideoSeekBarMaxTime;
    private IPlayButton mPlayBtn;
    private SeekBar mVideoSeekBar, mVolumeSeekBar;
    private RelativeLayout mAdverLayout, mMaxAdverLayout;
    private LinearLayout mVideoControlBar;
    private Context mContext;
    private String mUri;
    private boolean controlIsShow = true;//控制器在是否显示
    private boolean isTouchProgress = false;//正在滑动progress
    private boolean isLock;
    private boolean isKeep = false; // 是否开启流量检测
    private boolean isStart = false; // 是否开始
    private Handler mControlHandler;
    private Handler mUiHandler;
    private boolean isShowVolumeControl;
    private boolean isTouchLRScreen;
    private int oldVolumeProgress;
    private int oldLiangduProgress;
    private Activity mActivity;
    private OnClickListener onBackPressListener;
    private int mVideoViewHeight, mVideoViewWidth;
    protected boolean mIsMute;//是否静音


    /**
     * 更新（播放进度等等）
     */
    private int oldDuration = -1111;
    private int videoMaxDuration = -11;
    private boolean _startPlay = false;

    private Runnable mUiRunnable = new Runnable() {
        @Override
        public void run() {
            if (isStart) {
                updateUI();
            } else {
                mCenterPlayBtn.setVisibility(VISIBLE);
                mProgressBar.setVisibility(INVISIBLE);
            }
        }
    };

    /**
     * 控制器自动关闭
     */
    private Runnable mControlRunnable = new Runnable() {
        @Override
        public void run() {
            if (controlIsShow && !isTouchProgress) {
                changeControl();
            }
        }
    };

    /**
     * （亮度/声音）
     */
    private Runnable mLRControlRunnable = new Runnable() {
        @Override
        public void run() {
            if (isShowVolumeControl && !isTouchLRScreen) {
                changeVolumeControl();
            }
        }
    };
    private OnMediaListener mediaListener;


    public IVideoPlayer(Context context) {
        this(context, null);
    }

    public IVideoPlayer(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IVideoPlayer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.nur_video_layout, this);
        Settings mSettings = new Settings(context);
        mUiHandler = new Handler();
        mControlHandler = new Handler();
        initLayout();
        am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);// 3
        float current = am.getStreamVolume(AudioManager.STREAM_MUSIC);
        float a = maxVolume / 200;
        oldVolumeProgress = (int) (current / a);

        mVideoView.setOnPreparedListener(preparedListener);
//        mOnMediaPlayer = (OnMediaPlayer) mContext;
        openVideoError();
    }


    /**
     * 初始化View
     */
    private void initLayout() {
        mVideoView = findViewById(R.id.nur_ijk_video_player);
        mVideoView.setHudView((TableLayout) findViewById(R.id.hud_view));

        mTitleView = findViewById(R.id.nur_videoName);
        mVolumeIV = findViewById(R.id.nur_video_ktvIv);

        mPlayBtn = findViewById(R.id.nur_video_playIv);
        mScreenView = findViewById(R.id.nur_video_changeWindowTv);
        mBackIv = findViewById(R.id.nur_video_backIv);

        mTitleControl = findViewById(R.id.nur_video_toolbarControl);
        mBottomControl = findViewById(R.id.nur_video_bottomControl);
        mLockImage = findViewById(R.id.nur_video_view_LockIv);
        mRCImage = findViewById(R.id.nur_video_view_RC_btn);
        mAdverLayout = findViewById(R.id.nur_video_adver_layout);
        mMaxAdverLayout = findViewById(R.id.nur_video_max_adver_layout);
        mLayoutBox = findViewById(R.id.nur_ijk_video_player_box);

        mVideoDurationTv = findViewById(R.id.nur_video_videoSeekTv);
        mVideoMaxLenTv = findViewById(R.id.nur_video_videoDur);

        mCenterPlayBtn = findViewById(R.id.nur_video_centerPlayBtn);

        mVideoSeekBar = findViewById(R.id.nur_video_seekBar);

        mBgImage = findViewById(R.id.nur_video_bgImage);


        mCenterSBLayout = findViewById(R.id.nur_videoSeekBarBox);
        mVideoSeekBarImage = findViewById(R.id.nur_videoSeekBarImage);
        mVideoSeekBarTimeTv = findViewById(R.id.nur_videoSeekBarTimeTv);
        mVideoSeekBarMaxTime = findViewById(R.id.nur_videoSeekBarMaxTime);

        mProgressBar = findViewById(R.id.nur_video_progressBar);

        mVideoControlBar = findViewById(R.id.nur_video_control_bar);

        mLayoutBox.setOnTouchListener(new IOnTouch(mContext, nurTouchListener));
        mLockImage.setOnClickListener(this);
        mCenterPlayBtn.setOnClickListener(this);
        mPlayBtn.setOnClickListener(this);
        mScreenView.setOnClickListener(this);

        mVideoSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);

        mVolumeControl = findViewById(R.id.nur_video_volumeControl);
        mVolumeSeekBar = findViewById(R.id.nur_volumeSeekBar);
        volumeIcon = findViewById(R.id.nur_video_volumeIcon);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.nur_video_centerPlayBtn) {
            start();
        } else if (id == R.id.nur_video_view_LockIv) {
            controlIsShow = true;
            changeControl();
            isLock = !isLock;
            if (isLock)
                mLockImage.setImageResource(R.mipmap.nur_ic_lock);
            else
                mLockImage.setImageResource(R.mipmap.nur_ic_unlock);
        } else if (id == R.id.nur_video_playIv) {
            if (!isLock) {
                if (mVideoView.isPlaying())
                    pause();
                else start();
            }
        } else if (id == R.id.nur_video_backIv) {
            if (onBackPressListener != null) {
                onBackPressListener.onClick(v);
            }
        }
    }

    /**
     * 更新（播放进度等等）
     */
    private void updateUI() {
        int progress = mVideoView.getCurrentPosition();
        boolean playing = mVideoView.isPlaying();
        if (playing) {
            if (oldDuration == progress && videoMaxDuration != progress) {
                mProgressBar.setVisibility(View.VISIBLE);
                mUiHandler.postDelayed(mUiRunnable, 50);
                return;
            } else {
                mBgImage.setVisibility(GONE);
                if (mProgressBar.getVisibility() != INVISIBLE)
                    mProgressBar.setVisibility(View.INVISIBLE);
            }
        }
        oldDuration = progress;
        if (!isTouchProgress) {
            mVideoDurationTv.setText(stringForTime(progress));
            mVideoSeekBar.setProgress(progress);
            if (mediaListener != null) {
                mediaListener.onProgress(progress, videoMaxDuration);
            }
        }
        if (playing) {
            if (mediaListener != null && !_startPlay) {
                mediaListener.onStart();
            }
            if (mOnMediaPlayer == null) {
                mOnMediaPlayer = (OnMediaPlayer) mContext;
            }
            _startPlay = true;
        }
        if (playing || (!_startPlay && progress != videoMaxDuration)) {
            if (mCenterPlayBtn.getVisibility() != INVISIBLE) {
                mCenterPlayBtn.setVisibility(INVISIBLE);
                mPlayBtn.change(false);
            }
            mUiHandler.postDelayed(mUiRunnable, 50);
        } else {
            mCenterPlayBtn.setVisibility(VISIBLE);
            mPlayBtn.change(true);
            mOnMediaPlayer = null;
            if (mediaListener != null)
                mediaListener.onEndPlay();
        }
    }

    /**
     * 背景色
     */
    public void setBgColor(@ColorInt int color) {
        mLayoutBox.setBackgroundColor(color);
    }

    /**
     * 获取背景view
     *
     * @return
     */
    public ImageView getThumbImageView() {
        return mBgImage;
    }

    /**
     * 获取播放状态
     * @return
     */
    public boolean getPlaying(){
        return mVideoView.isPlaying();
    }


    /**
     * 打开错误提示
     */
    public void openVideoError() {
        mVideoView.setOnErrorListener(errorListener);
    }

    /**
     * 设置控制条 是否显示 默认不显示
     *
     * @param b true:显示
     */
    public void setVideoControlBar(boolean b) {
        if (b)
            mVideoControlBar.setVisibility(VISIBLE);
    }


    /**
     * 声道控制器
     *
     * @return
     */
    public ImageView getVolumeImageView() {
        return mVolumeIV;
    }

    /**
     * back image view
     */
    public View getBackIv() {
        return mBackIv;
    }

    /**
     * 小广告view
     */
    public RelativeLayout getMinADLayout() {
        return mAdverLayout;
    }

    /**
     * 小广告view
     */
    public void setMinADLayout(View v) {
        mAdverLayout.removeAllViews();
        mAdverLayout.addView(v);
    }

    /**
     * 大广告view
     */
    public RelativeLayout getMaxADLayout() {
        return mMaxAdverLayout;
    }

    /**
     * 大广告view
     */
    public void setMaxADLayout(View v) {
        mMaxAdverLayout.addView(v);
    }

    /**
     * 加载时显示的图片
     *
     * @return
     */
    public ImageView getRCImage() {
        mRCImage.setVisibility(VISIBLE);
        return mRCImage;
    }

    /**
     * IjkVideoView
     */
    public IjkVideoView getIjkVideoView() {
        return mVideoView;
    }

    /**
     * 获取media player
     */
    public IMediaPlayer getMediaPlayer() {
        return iMediaPlayer;
    }

    /**
     * 全屏按钮
     */
    public ImageView getScreenView() {
        return mScreenView;
    }

    /**
     * 是否开启流量检测
     *
     * @param isOpen true:开启  false:关闭
     */
    public void isOpenKeep(boolean isOpen) {
        isKeep = isOpen;
    }

    /**
     * 关闭全部控制器
     */
    public void hideControllers() {
        if (controlIsShow) {
            changeControl();
        }

        mProgressBar.setVisibility(View.INVISIBLE);
        mCenterPlayBtn.setVisibility(INVISIBLE);
    }

    /**
     * 打开全部控制器
     */
    public void showControllers() {
        if (!controlIsShow)
            changeControl();
    }

    /**
     * 返回按钮点击
     */
    public void setOnBackPressListener(OnClickListener onBackPressListener) {
        this.onBackPressListener = onBackPressListener;
    }

    /**
     * Sets video URI.
     *
     * @param
     */
    public void initPlayer(Activity activity, String title) {
        if (title != null)
            mTitleView.setText(title);
        mActivity = activity;
    }

    /**
     * Sets video path.
     *
     * @param path the path of the video.
     */
    public void setVideoPath(String path) {
        mUri = path;
    }

    /**
     * 更新播放地址
     *
     * @param uri
     */
    public void upVideoPath(Uri uri) {
        isStart = true;
        mVideoView.pause();
        stopVideoInfo();

        if (mediaListener != null) {
            mediaListener.onPause();
        }

        mVideoView.setVideoURI(uri);
        mUiHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mCenterPlayBtn.setVisibility(INVISIBLE);
                mProgressBar.setVisibility(VISIBLE);
                mPlayBtn.change(false);
            }
        }, 60);
    }

    public void setTitle(String title) {
        mTitleView.setText(title);
    }


    /**
     * 开始播放
     */
    public void start() {
        start(0);
    }

    /**
     * 开始播放
     */
    public void start(int progress) {
        if (mUri != null) {
            mVideoView.setVideoURI(Uri.parse(mUri));
            mUiHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mCenterPlayBtn.setVisibility(INVISIBLE);
                    mProgressBar.setVisibility(VISIBLE);
                    mPlayBtn.change(false);
                }
            }, 60);
            isStart = true;
        }

        if (progress > 0) {
            mVideoView.seekTo(progress);
        }
        mVideoView.start();

        autoDismiss();
        mUiHandler.removeCallbacks(mUiRunnable);
        mUiHandler.postDelayed(mUiRunnable, 50);

        openVideoError();
        if (mVideoView.isPlaying()) {
            isStart = true;
        }

        if (isKeep) {
            startVideoInfo();
        }
    }

    /**
     * 暂停
     */
    public void pause() {
        mUiHandler.removeCallbacks(mUiRunnable);
        mCenterPlayBtn.setVisibility(VISIBLE);
        mProgressBar.setVisibility(INVISIBLE);
        mPlayBtn.change(true);
        mVideoView.pause();

        if (mediaListener != null) {
            mediaListener.onPause();
        }
//        isStart = false;
        stopVideoInfo();

        mOnMediaPlayer = null;
    }

    /**
     * 视频加载完成, 准备好播放视频的回调
     */
    private IMediaPlayer.OnPreparedListener preparedListener = new IMediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(IMediaPlayer mp) {
            iMediaPlayer = mp;

            videoMaxDuration = mVideoView.getDuration();
            mVideoViewHeight = mp.getVideoHeight();
            mVideoViewWidth = mp.getVideoWidth();
            mVideoSeekBar.setMax(videoMaxDuration);
            mVideoMaxLenTv.setText(stringForTime(videoMaxDuration));
            mProgressBar.setVisibility(INVISIBLE);
            mUiHandler.postDelayed(mUiRunnable, 50);
        }
    };

    private IMediaPlayer.OnErrorListener errorListener = new IMediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(IMediaPlayer mp, int what, int extra) {
            if (what == -10000) {
                Toast.makeText(mContext, "网路未连接，请检查网络设置", Toast.LENGTH_SHORT).show();
                pause();
                return true;
            }
            return false;
        }
    };

    /**
     * 获取视频高宽度
     */
    public int[] getVideoWH() {
        return new int[]{mVideoViewWidth, mVideoViewHeight};
    }

    /**
     * 截图，暂不支持SurfaceView
     */
    public Bitmap doScreenShot() {
        return mVideoView.doScreenShot();
    }

    /**
     * 获取ObjectAnimator
     */
    private ObjectAnimator getObjectAnimator(float start, float end, String propertyName, View view) {
        return ObjectAnimator.ofFloat(view, propertyName, start, end);
    }

    /**
     * 获取ObjectAnimators
     */
    private List<Animator> getObjectAnimator(float start, float end, String propertyName, View... view) {
        if (view == null) {
            return null;
        }
        List<Animator> animators = new ArrayList<>();
        int length = view.length;
        for (int i = 0; i < length; i++) {
            animators.add(getObjectAnimator(start, end, propertyName, view[i]));
        }
        return animators;
    }

    /**
     * 开始动画
     */
    private void startAnim(View view, float start, float end, String propertyName) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(view, propertyName, start, end);
        anim.setDuration(350);
        anim.start();
    }

    /**
     * 开始动画
     */
    private void startAnim(List<Animator> animators) {
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(animators);
        animatorSet.setDuration(350);
        animatorSet.start();
    }


    /**
     * 显示(隐藏)控制器
     */
    private void changeControl() {
        int dp_56 = dip2px(-56);

        int anim1Start = 0;
        int anim2Start = 0;
        int anim3Start = 0;

        int rcAnimStart = 0;

        int anim1End = dip2px(40);
        int anim2End = dip2px(-66);
        int anim3End = dp_56;

        int rcAnimEnd = dip2px(56);

        if (!controlIsShow) {//要显示（现在的状态是隐藏）
            anim1Start = anim1End;
            anim2Start = anim2End;
            anim3Start = anim3End;

            rcAnimStart = rcAnimEnd;

            anim1End = 0;
            anim2End = 0;
            anim3End = 0;
            rcAnimEnd = 0;
        }
        String translationY = "translationY";
        String translationX = "translationX";
        ObjectAnimator objectAnimator = getObjectAnimator(anim3Start, anim3End, translationX, mLockImage);
        if (isLock) {
            ArrayList<Animator> animators = new ArrayList<>();
            animators.add(objectAnimator);
            startAnim(animators);
        } else {
            List<Animator> animator = getObjectAnimator(anim1Start, anim1End, translationY, mAdverLayout, mBottomControl);
            animator.add(getObjectAnimator(anim2Start, anim2End, translationY, mTitleControl));
            animator.add(getObjectAnimator(rcAnimStart, rcAnimEnd, translationX, mRCImage));
            animator.add(objectAnimator);
            startAnim(animator);
        }
        controlIsShow = !controlIsShow;
        autoDismiss();
    }

    /**
     * 3秒后知道关闭（隐藏）
     * 显示的话3秒后知道隐藏
     */
    public void autoDismiss() {
        if (controlIsShow) {
            mControlHandler.removeCallbacks(mControlRunnable);
            mControlHandler.postDelayed(mControlRunnable, 3000);
        }
    }

    /**
     * 3秒后知道关闭（隐藏）
     * 显示的话3秒后知道隐藏
     */
    private void autoDismiss(Runnable runnable) {
        if (runnable != null) {
            mControlHandler.removeCallbacks(runnable);
            mControlHandler.postDelayed(runnable, 1000);
        }
    }

    /**
     * seekBarChangeListener
     */
    private SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (isTouchProgress) {
                videoSeekChange(progress);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            isTouchProgress = true;
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            mVideoView.seekTo(seekBar.getProgress());
            autoDismiss();
            hideVideoSeek();
            isTouchProgress = false;
        }
    };

    /**
     * 显示中间seek
     */
    private void videoSeekChange(int progress) {
        if (mCenterSBLayout.getVisibility() != VISIBLE) {
            mCenterSBLayout.setVisibility(VISIBLE);
            _playBtnIsShow = mCenterPlayBtn.getVisibility() == VISIBLE;
            mCenterPlayBtn.setVisibility(INVISIBLE);
        }
        if (progress > oldProgress) {
            mVideoSeekBarImage.setImageResource(R.mipmap.nur_ic_kuaijin_r);
        } else {
            mVideoSeekBarImage.setImageResource(R.mipmap.nur_ic_kuaijin);
        }
        int max = videoMaxDuration;
        if (progress < 0) {
            progress = 0;
        } else if (progress > max) {
            progress = max;
        }

        String play_time = stringForTime(progress);
        String play_sum_time = stringForTime(max);
        mVideoSeekBarTimeTv.setText(play_time);
        mVideoSeekBarMaxTime.setText("/ " + play_sum_time);

        if (oldProgress == 0) {
            oldProgress = progress;
        }
        moveProgress = progress;
    }

    private int oldProgress = 0;
    private int moveProgress;
    private boolean _playBtnIsShow = false;

    /**
     * 关闭中间seek
     */
    private void hideVideoSeek() {
        oldProgress = 0;
        mVideoView.seekTo(moveProgress);
        if (_playBtnIsShow) {
            mCenterPlayBtn.setVisibility(VISIBLE);
        }
        mCenterSBLayout.setVisibility(INVISIBLE);
    }

    /**
     * NurTouchListener
     * （单）双击-滑动等等
     */
    private IOnTouch.ITouchListener nurTouchListener = new IOnTouch.ITouchListener() {
        @Override
        public void onClick() {
            changeControl();
        }

        @Override
        public void onDoubleClick() {
            if (!isLock) {
                if (mVideoView.isPlaying())
                    pause();
                else start();
            }
        }

        @Override
        public void onMoveSeek(float f) {
            if (isLock || !mVideoView.isPlaying()) {
                return;
            }
            int progress = (int) (mVideoView.getCurrentPosition() + (100 * f));
            videoSeekChange(progress);
        }

        @Override
        public void onMoveLeft(float f) {
            if (isLock) {
                return;
            }
            setVolume(f);
        }

        @Override
        public void onMoveRight(float f) {
            if (isLock) {
                return;
            }
            int progress = (int) f + oldLiangduProgress;
            if (progress > MAX_LIANG_DU) {
                progress = (int) MAX_LIANG_DU;
            }
            if (progress < 0) {
                progress = 1;
            }
            mVolumeSeekBar.setMax((int) MAX_LIANG_DU);
            setWindowBrightness(progress);
            setProgress(progress, R.mipmap.nur_ic_brightness);
        }

        @Override
        public void onActionUp(int changeType) {
            if (isLock) {
                return;
            }
            if (changeType == IOnTouch.changeTypeVideoSeek) {
                hideVideoSeek();
            } else if (changeType == IOnTouch.changeTypeLiangdu) {
                oldLiangduProgress = mVolumeSeekBar.getProgress();
                isTouchLRScreen = false;
                autoDismiss(mLRControlRunnable);
            } else if (changeType == IOnTouch.changeTypeVolume) {
                isTouchLRScreen = false;
                autoDismiss(mLRControlRunnable);
                oldVolumeProgress = mVolumeSeekBar.getProgress();
            }
        }
    };

    /**
     * 设置亮度
     */
    private void setWindowBrightness(int brightness) {
        if (mActivity == null) return;
        Window window = mActivity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.screenBrightness = brightness / 255.0f;
        window.setAttributes(lp);
    }

    /**
     * 显示-隐藏 ---亮度&声音
     */
    public void changeVolumeControl() {
        float start = dip2px(80);
        float end = dip2px(-30);
        if (!isShowVolumeControl) {
            start = end;
            end = dip2px(80);
        }
        startAnim(mVolumeControl, start, end, "translationY");
        isShowVolumeControl = !isShowVolumeControl;
    }

    /**
     * 设置静音
     */
    public void setMute(boolean isMute) {
        this.mIsMute = isMute;
        if (iMediaPlayer != null) {
            float volume = isMute ? 0.0f : 1.0f;
            iMediaPlayer.setVolume(volume, volume);
        }
    }

    /**
     * 是否处于静音状态
     */
    public boolean isMute() {
        return mIsMute;
    }

    /**
     * 声音
     */
    public void setVolume(float f) {
        mVolumeSeekBar.setMax(200);

        int progress = (int) f + oldVolumeProgress;
        int res = R.mipmap.nur_ic_volume;
        if (progress <= 0) {
            res = R.mipmap.nur_ic_volume_x;
        }
        setProgress(progress, res);
        if (am == null) {
            return;
        }
        am.setStreamVolume(AudioManager.STREAM_MUSIC, (int) ((maxVolume / 200) * progress), 0);
    }

    /**
     * 监听声音大小
     */
    public boolean onKeyDown(int keyCode) {
        int value = 10;
        boolean ret = false;
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                setVolume(value);
                oldVolumeProgress += value;
                ret = true;
                break;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                setVolume(-value);
                oldVolumeProgress -= value;
                ret = true;
                break;
        }
        isTouchLRScreen = false;
        autoDismiss(mLRControlRunnable);
        return ret;
    }

    /**
     * 亮度（声音）的seek bar
     */
    private void setProgress(int progress, int res) {
        isTouchLRScreen = true;
        volumeIcon.setImageResource(res);
        mVolumeSeekBar.setProgress(progress);
        if (!isShowVolumeControl)
            changeVolumeControl();
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    private int dip2px(float dpValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    StringBuilder mFormatBuilder = new StringBuilder();
    Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());

    /**
     * 将长度转换为时间
     *
     * @param timeMs
     * @return
     */
    private String stringForTime(int timeMs) {
        int totalSeconds = timeMs / 1000;

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;
        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }

    //      /**
//     * release
//     */
    public void stopPlayback() {
        mVideoView.stopPlayback();
        mVideoView.release(true);
        mVideoView.stopBackgroundPlay();
        IjkMediaPlayer.native_profileEnd();

        Log.i("IMediaPlayer", "播放结束====================================================");
        stopVideoInfo();
    }

    /**
     * 监听（播放，暂停）
     */
    public void setOnMediaListener(OnMediaListener mediaListener) {
        this.mediaListener = mediaListener;
    }


    private Runnable runnable;
    private long delayMillis = 500;
    public static final int UPDATE_VIDEO_INFO = 1;
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            switch (message.what) {
                case UPDATE_VIDEO_INFO: {
                    IMediaPlayer mp = null;
                    if (iMediaPlayer == null)
                        break;
                    if (iMediaPlayer instanceof IjkMediaPlayer) {
                        mp = iMediaPlayer;
                    } else if (iMediaPlayer instanceof MediaPlayerProxy) {
                        MediaPlayerProxy proxy = (MediaPlayerProxy) iMediaPlayer;
                        IMediaPlayer internal = proxy.getInternalMediaPlayer();
                        if (internal != null && internal instanceof IjkMediaPlayer)
                            mp = internal;
                    } else if (iMediaPlayer instanceof IjkExoMediaPlayer) {
                        mp = iMediaPlayer;
                    }

                    IjkMediaPlayer mps = (IjkMediaPlayer) mp;
                    long tcpSpeeds = mps.getTcpSpeed();
                    //                    Log.i("IMediaPlayer", "onPrepared: Handler" + String.format(Locale.US, "%s", formatedSpeed(tcpSpeeds)));

                    if (mOnMediaPlayer != null)
                        mOnMediaPlayer.onMediaPlayer(mps);
                }
            }
            return true;
        }
    });

    /**
     *
     */
    public void startVideoInfo() {
        runnable = new Runnable() {
            @Override
            public void run() {
                mHandler.obtainMessage(UPDATE_VIDEO_INFO).sendToTarget();
                mHandler.postDelayed(runnable, delayMillis);
            }
        };
        mHandler.post(runnable);
    }

    /**
     * 停止视频检测，建议在onDestroy()方法中调用
     */
    public void stopVideoInfo() {
        mHandler.removeCallbacks(runnable);
        mHandler.removeMessages(UPDATE_VIDEO_INFO);
    }


    public static String formatedSize(long bytes) {
        if (bytes >= 100 * 1000) {
            return String.format(Locale.US, "%.2f MB", ((float) bytes) / 1000 / 1000);
        } else if (bytes >= 100) {
            return String.format(Locale.US, "%.1f KB", ((float) bytes) / 1000);
        } else {
            return String.format(Locale.US, "%d B", bytes);
        }
    }

    public static String formatedDurationMilli(long duration) {
        if (duration >= 1000) {
            return String.format(Locale.US, "%.2f sec", ((float) duration) / 1000);
        } else {
            return String.format(Locale.US, "%d msec", duration);
        }
    }

    /**
     * 下载速度格式化显示
     *
     * @param bytes 毫秒级
     * @return
     */
    public static String formatedSpeed(long bytes) {
        if (bytes <= 0) {
            return "0 B/s";
        }

        float bytes_per_sec = ((float) bytes) * 1000.f / 1000;
        if (bytes_per_sec >= 1000 * 1000) {
            return String.format(Locale.US, "%.2f MB/s", ((float) bytes_per_sec) / 1000 / 1000);
        } else if (bytes_per_sec >= 1000) {
            return String.format(Locale.US, "%.1f KB/s", ((float) bytes_per_sec) / 1000);
        } else {
            return String.format(Locale.US, "%d B/s", (long) bytes_per_sec);
        }
    }

    public static String formatedSpeed(long bytes, long elapsed_milli) {
        if (elapsed_milli <= 0) {
            return "0 B/s";
        }
        if (bytes <= 0) {
            return "0 B/s";
        }
        float bytes_per_sec = ((float) bytes) * 1000.f / elapsed_milli;
        if (bytes_per_sec >= 1000 * 1000) {
            return String.format(Locale.US, "%.2f MB/s", ((float) bytes_per_sec) / 1000 / 1000);
        } else if (bytes_per_sec >= 1000) {
            return String.format(Locale.US, "%.1f KB/s", ((float) bytes_per_sec) / 1000);
        } else {
            return String.format(Locale.US, "%d B/s", (long) bytes_per_sec);
        }
    }
}
